﻿using MqttBased;
using System;
using System.Threading.Tasks;
using BusinessSample;
using Newtonsoft.Json;

namespace PublishClient
{
    class Program
    {
        private static MqttClientProvider _provider;
        static async Task Main(string[] args)
        {
            Console.WriteLine("这是一个发布客户端案例，按任意键启动客户端");
            Console.ReadKey();
            _provider = new MqttClientProvider();
            var setting = new MqttSettingConfig().CreateDefaultSetting();
            await _provider.CreateClientInstance(setting);
            await _provider.SubscribeTopic("HardwareCallBack", null);
            Console.WriteLine("按q键退出客户端,按g开闸,按c传递车辆信息,按w传递载重信息,按其他没有回调");
            while (true)
            {
                var str = Console.ReadLine();
                if (str == "q")
                {
                    await _provider.CloseClient();
                    break;
                }
                else
                {
                    switch (str)
                    {
                        case "g":
                            var result = await _provider.SendMessage<BusinessMessage>(new MqttMessage()
                            {
                                MessageBody = new BusinessMessage()
                                {
                                    MachineKey = "Lkrv9yLv0z8KGjEJgTCfufx3VN3i2k4tjMOKqj0zFw9UMR9kWEgv4YaMDIupgZJm74zsvAUUCaK6ScSdQ7YbOJxULM0MxXm4Imc",
                                    HandlerType = HandlerType.OpenGate,
                                },
                                NeedReceived = true,
                                ReceivedTopicName = "HardwareCallBack",
                                TaskId = Guid.NewGuid(),
                                TopicName = "HardwareCall"
                            });
                            if (result != null)
                            {
                                Console.WriteLine($"订阅端：{JsonConvert.SerializeObject(result)}");
                            }
                            break;
                        case "c":
                            var carresult = await _provider.SendMessage<CarInfo>(new MqttMessage()
                            {
                                MessageBody = new CarInfo()
                                {
                                    MachineKey = "Lkrv9yLv0z8KGjEJgTCfufx3VN3i2k4tjMOKqj0zFw9UMR9kWEgv4YaMDIupgZJm74zsvAUUCaK6ScSdQ7YbOJxULM0MxXm4Imc",
                                    CarNum = "川A00101",
                                    CarNumColor = "蓝色",
                                    MessageType = 0,
                                    HandlerType = HandlerType.GetCarInfo
                                },
                                NeedReceived = true,
                                ReceivedTopicName = "HardwareCallBack",
                                TaskId = Guid.NewGuid(),
                                TopicName = "WebInfoCall"
                            });
                            if (carresult != null)
                            {
                                Console.WriteLine($"订阅端：{JsonConvert.SerializeObject(carresult)}");
                            }
                            break;
                        case "w":
                            var weightresult = await _provider.SendMessage<CarInfo>(new MqttMessage()
                            {
                                MessageBody = new CarInfo()
                                {
                                    MachineKey = "Lkrv9yLv0z8KGjEJgTCfufx3VN3i2k4tjMOKqj0zFw9UMR9kWEgv4YaMDIupgZJm74zsvAUUCaK6ScSdQ7YbOJxULM0MxXm4Imc",
                                    Weight = (decimal)5.64,
                                    MessageType = 1,
                                    HandlerType = HandlerType.GetCarWeight
                                },
                                NeedReceived = true,
                                ReceivedTopicName = "HardwareCallBack",
                                TaskId = Guid.NewGuid(),
                                TopicName = "WebInfoCall"
                            });
                            if (weightresult != null)
                            {
                                Console.WriteLine($"订阅端：{JsonConvert.SerializeObject(weightresult)}");
                            }
                            break;
                    }
                }
            }
        }
    }

}
